﻿using Nemerle;
using Nemerle.Collections;
using Nemerle.Peg;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Nemerle2.Compiler
{
  /// <summary>
  /// Описывает набор секций CustomAttribute-ов разобранных из исходников или квази-цитат.
  /// </summary>
  [Record]
  public class PCustomAttributes : PAst, ICustomAttributes
  {
    public Sections : LocatedList[PCustomAttributesSection];

    public CustomAttributesAst : list[PCustomAttribute]
    {
      [Memoize] get
      {
        def filterAttrs(sec : PCustomAttributesSection) : Seq[PCustomAttribute]
        {
          | Content(_, attrs) => attrs
          | _ => []
        }
        Sections.SelectMany(filterAttrs).NToList()
      }
    }

    #region ICustomAttributes  Members

    public CustomAttributeNames : list[string]
    {
      [Memoize] get { CustomAttributesAst.Map(_.ToString()) }
    }

    public CustomAttribute[attributeType : ITypeInfo] : list[ICustomAttribute]
    {
      get
      {
        _ = attributeType;
        throw System.NotImplementedException()
      }
    }

    public CustomAttribute[attributeFullName : string] : list[ICustomAttribute]
    {
      get
      {
        _ = attributeFullName;
        throw System.NotImplementedException()
      }
    }

    public IsCustomAttributeDefined(attributeType : ITypeInfo) : bool
    {
      _ = attributeType;
      throw System.NotImplementedException()
    }

    public IsCustomAttributeDefined(attributeFullName : string) : bool
    {
      _ = attributeFullName;
      throw System.NotImplementedException()
    }

    /// Can contains nulls!
    private CustomAttributesArray : array[ICustomAttribute]
    {
      [Memoize]
      get
      {
        def to_ICustomAttribute(attr : PCustomAttribute) : ICustomAttribute
        {
          | PCustomAttribute.Content as a => PCustomAttributeMetadataProvider(a)
          | _ => null
        }
        CustomAttributesAst.MapToArray(to_ICustomAttribute)
      }
    }

    public CustomAttributes : list[ICustomAttribute]
    {
      [Memoize] get { CustomAttributesArray.Filter(_ != null) }
    }

    #endregion
  }
}
